/**
 * 登录入口文件，在这里根据终端环境（微信小程序/微信公众号/h5）分别进入各自的登入入口
 * @author 许明浩 微信: muaiyuese
 */
import { Loading } from "@nutui/nutui-react-taro";
import { View } from "@tarojs/components";
import { useEffect } from "react";
import { redirectTo } from "@tarojs/taro";
import { isWeChat } from "@/lib/platform";
import { getSearchParams } from "@/lib/get-search-params";
import { instance as Log } from "@/lib/log";

function Auth() {
  /**
   * 跳转登录逻辑
   * @param {}
   * @return
   */
  const formatBackUrl = (url: string) => {
    // Log.log(
    //   url +
    //     `?backurl=${getSearchParams("backurl")}&isSwitchTab=${getSearchParams(
    //       "isSwitchTab"
    //     )}`
    // );
    return (
      url +
      `?backurl=${getSearchParams("backurl")}&isSwitchTab=${getSearchParams(
        "isSwitchTab"
      )}`
    );
  };
  const goLogin = () => {
    if (process.env.TARO_ENV === "weapp") {
      redirectTo({
        url: formatBackUrl("/sub_common/pages/auth/wechat-mini-program/index"),
      });
    }
    if (process.env.TARO_ENV === "h5") {
      if (isWeChat()) {
        redirectTo({
          url: formatBackUrl("/sub_common/pages/auth/wechat/index"),
        });
      } else {
        redirectTo({
          url: formatBackUrl("/sub_common/pages/auth/login/index"),
        });
      }
    }
  };

  useEffect(() => {
    setTimeout(() => {
      goLogin();
    }, 30);
  }, []);

  return (
    <View className="w-full h-[100vh] flex justify-center items-center">
      <Loading />
    </View>
  );
}

export default Auth;
